package com.leadership.api.web.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;
import com.leadership.api.handler.UMUHandler;
import com.leadership.api.service.CertificateService;
import com.leadership.api.service.IdmService;
import com.leadership.entity.idm.IdmUser;
import com.leadership.entity.idm.Organization;
import com.leadership.entity.umu.UMUElementStudent;
import com.leadership.entity.umu.UMUExamData;

/**
 * 
 * @author dan
 *
 */
@RestController
@RequestMapping("")
public class IdmController {

    private static final Logger logger = LoggerFactory.getLogger(IdmController.class);

    @Autowired
    private IdmService idmService;

    @Autowired
    private CertificateService certificateService;

    @Autowired
    private UMUHandler uMUHandler;

    @RequestMapping(value = "/search-umu", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult searchUmu(@RequestParam("elementId") String elementId) {

        HttpResult httpResult = new HttpResult();

        try {

            List<UMUExamData> data = uMUHandler.listExamData(elementId);
            httpResult.success().data("umuData", data);

        } catch (Exception e) {
            logger.error("#listUsers error - ", e);
            httpResult.fail("500").message("查询umu失败");
        }

        return httpResult;
    }

    /**
     * 
     * @param elementUrl
     * @return
     */
    @RequestMapping(value = "/search-element", method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult searchUmuElementStudent(@RequestBody Map<String, String> params) {

        HttpResult httpResult = new HttpResult();

        try {

            List<UMUElementStudent> data = uMUHandler.listUMUElementStudent(params.get("url"));
            httpResult.success().data("elementStudent", data);

        } catch (Exception e) {
            logger.error("#listUsers error - ", e);
            httpResult.fail("500").message("查询umu失败");
        }

        return httpResult;
    }

    /**
     * 
     * @param elementUrl
     * @return
     */
    @RequestMapping(value = "/update-learningmap-result", method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult updateLearningMapresult() {

        HttpResult httpResult = new HttpResult();

        try {

            idmService.statLearningMap();
            httpResult.success().message("更新完成率成功");

        } catch (Exception e) {
            logger.error("#listUsers error - ", e);
            httpResult.fail("500").message("更新完成率失败");
        }

        return httpResult;
    }

    /**
     * 
     * @return
     */
    @RequestMapping(value = "/course-learning-update", method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult updateCourseLearning() {

        HttpResult httpResult = new HttpResult();

        try {

            certificateService.updateOutCourse();

            httpResult.success().message("更新外部课程成功");

        } catch (Exception e) {
            logger.error("#listUsers error - ", e);
            httpResult.fail("500").message("更新外部课程失败");
        }

        return httpResult;
    }

    /**
     * 
     * @return
     */
    @RequestMapping(value = "/userproject-update", method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult userprojectUpdate() {

        HttpResult httpResult = new HttpResult();

        try {

            certificateService.updateUserProject();

            httpResult.success().message("更新外部课程成功");

        } catch (Exception e) {
            logger.error("#listUsers error - ", e);
            httpResult.fail("500").message("更新外部课程失败");
        }

        return httpResult;

    }

    /**
     * 
     * @return
     */
    @RequestMapping(value = "/idm-users", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult listUsers() {

        HttpResult httpResult = new HttpResult();

        try {

            Map<String, List<IdmUser>> idmUsers = idmService.listIdmUser();

            httpResult.success().data("idmUsers", idmUsers).message("list idm user success");

        } catch (Exception e) {
            logger.error("#listUsers error - ", e);
            httpResult.fail("500").message("list idm user failed");
        }

        return httpResult;

    }

    /**
     * @return
     */
    @RequestMapping(value = "/idm-organizations", method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult listOrganizations() {

        HttpResult httpResult = new HttpResult();

        try {

            List<Organization> organizations = idmService.listIdmOrganization();
            httpResult.success().data("organizations", organizations).message("list idm organization success");

        } catch (Exception e) {
            logger.error("#listOrganizations error - ", e);
            httpResult.fail("500").message("list idm organization failed");
        }
        return httpResult;

    }

    /**
     * 
     * @return
     */
    @RequestMapping(value = "/create-org", method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreateOrg() {

        HttpResult httpResult = idmService.updateOrg();

        return httpResult;

    }

    /**
     * 
     * @return
     */
    @RequestMapping(value = "/create-user", method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreateUser() {

        HttpResult httpResult = idmService.updateUserByJob();

        return httpResult;

    }

    /**
     * 
     * @param fiString
     * @return
     */
    @RequestMapping(value = "/create-new-user", method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
        
        public @ResponseBody HttpResult onCreateUser(@RequestParam("filter") String fiString) {

        HttpResult httpResult = idmService.updateUser(fiString);
        logger.info("get final httpResult[{}]", JsonUtils.toJson(httpResult));
        return httpResult;
    }

    /**
     * 
     * @param fiString
     * @return
     */
    @RequestMapping(value = "/create-old-user", method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreateOldUser(@RequestParam("filter") String fiString) {

        HttpResult httpResult = idmService.updateUserWithNoNotification(fiString);
        return httpResult;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/update-all-user", method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
        public @ResponseBody HttpResult onUpdateAllUser() {

            HttpResult httpResult = idmService.updateAllUser();
            return httpResult;
        }

    /**
     * 
     * @param
     * @return
     */
    @RequestMapping(value = "/notice-learningmap", method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onNoticeLearningMap() {

        HttpResult httpResult = idmService.noticeLearningMap();
        return httpResult;
    }

    /**
     * 
     * @param orgno
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/create-org-user", method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreateOrgUser(@RequestParam("org") String orgno)
        throws UnsupportedEncodingException {

        HttpResult httpResult = idmService.updateOrgUser(orgno);
        return httpResult;
    }

}
